<%--
  @author Duminda Dharmakeerthi
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<script type="text/javascript">
    $(function() {
        $('#land-schedule-form').hide();
        initData();
        $('select#gnDivisionId').bind('change', function(evt1) {
            initData();
        });
    });

    $(document).ready(function () {
        $('.land-table-schedule').dataTable({
            "sPaginationType":"full_numbers",
            "bDestroy":true,
            "oLanguage":{
                "sZeroRecords":$('#no-record').val(),
                "sLengthMenu":$('#display').val() + " _MENU_ " + $('#records').val(),
                "sSearch":$('#search').val(),
                "sInfo":$('#showing').val() + " _START_ " + $('#to').val() + " _END_ " + $('#of').val() + " _TOTAL_ " + $('#entries').val(),
                "sInfoEmpty":$('#no-record').val(),
                "oPaginate":{
                    "sFirst":$('#first').val(),
                    "sLast":$('#last').val(),
                    "sNext":$('#next').val(),
                    "sPrevious":$('#previous').val()
                }
            },
            "fnDrawCallback":function () {
                if ($('.dataTables_paginate span span.paginate_button').size()) {
                    $('.dataTables_paginate')[0].style.display = "block";
                } else {
                    $('.dataTables_paginate')[0].style.display = "none";
                }
            }
        });
    });

    function initData() {
        var id = $('select#gnDivisionId').attr('value');
        //alert(id);
        var mode = "byGNDivision";
        //var type = "init";
        var type = "detail";
        $.getJSON('${pageContext.request.contextPath}/common/LandLookupService', {id:id, mode:mode, type:type}, function(data) {
            loadLandList(data.landList);
        });
    }

    $(function() {
        var pls_enter_task = $("#pls_enter_task").val();
        var pls_enter_valid_date = $("#pls_enter_date").val();
        $("#schedule-land-visit-plan").validate({
            rules:{
                task:{
                    required:true
                },
                endingDate:{
                    required: true,
                    isDateFormated:true
                }
            },
            messages:{
                task: {
                    required:pls_enter_task
                },
                endingDate:{
                    required: "<br/>" + pls_enter_valid_date
                }
            }
        });


    });

    function loadLandList(landInfo) {
        var opt = '';
        opt += '<thead>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#ref').val() + '</th>' +
                '<th class="left-align">' + $('#lname').val() + '</th>' +
                '<th class="center-align">' + $('#plan').val() + '</th>' +
                '<th class="center-align">' + $('#lot').val() + '</th>' +
                '<th class="center-align">' + $('#extent').val() + '</th>' +
            //'<th class="center-align">' + $('#select').val() + '</th>' +
                '<th class="center-align">&nbsp;</th>' +
                '</tr>' +
                '</thead>';
        if (landInfo.length > 0) {
            opt += '<tbody>';
            for (var i = 0; i < landInfo.length; i++) {
                var reference = (landInfo[i].referenceNumber > 0) ? landInfo[i].referenceNumber : $('#undifined').val();
                opt += '<tr>' +
                        '<td class="center-align">' + landInfo[i].id + '</td>' +
                        '<td>' + landInfo[i].nameOfTheLand + '</td>' +
                        '<td class="center-align">' + landInfo[i].planNumber + '</td>' +
                        '<td class="center-align">' + landInfo[i].lotNumber + '</td>' +
                        '<td class="center-align">' + landInfo[i].landExtent + '</td>' +
                        '<td class="center-align"><input title="' + $('#viewTooltip').val() + '" type="checkbox" name="scheduleLandList" value="' + landInfo[i].id + '" /></td>' +
                        '</tr>';
            }
            $('#land-schedule-form').show();
        } else {
            opt += '<tbody>';
            $('#land-schedule-form').hide();
        }
        opt += '</tbody>';
        $('.land-table').html(opt);
        $('.land-table').dataTable({
            "sPaginationType": "full_numbers",
            "bDestroy": true,
            "oLanguage": {
                "sZeroRecords": $('#no-record').val(),
                "sLengthMenu": $('#display').val() + " _MENU_ " + $('#records').val(),
                "sSearch": $('#search').val(),
                "sInfo": $('#showing').val() + " _START_ " + $('#to').val() + " _END_ " + $('#of').val() + " _TOTAL_ " + $('#entries').val(),
                "sInfoEmpty": $('#no-record').val(),
                "oPaginate": {
                    "sFirst": $('#first').val(),
                    "sLast": $('#last').val(),
                    "sNext": $('#next').val(),
                    "sPrevious": $('#previous').val()
                }
            },
            "fnDrawCallback": function() {
                if ($('.dataTables_paginate span span.paginate_button').size()) {
                    $('.dataTables_paginate')[0].style.display = "block";
                } else {
                    $('.dataTables_paginate')[0].style.display = "none";
                }
            }
        });
    }


</script>

<div id="land-registration-module-home-title" class="module-home-title">
    <s:a href="landRegistrationModule.do">
        <span><img src="${pageContext.request.contextPath}/images/land-reg.png" alt="Land Registration">
            <s:label value="%{getText('land_registration.label')}"/></span>
    </s:a>
    <span class="module-title-breaker"> > </span>
    <s:a href="scheduleLandVisits.do">
        <span><img src="${pageContext.request.contextPath}/images/schedule-task.png" alt="Schedule Land Visits">
            <s:label value="%{getText('schedule_land_visits.label')}"/></span>
    </s:a>

    <div id="back-button" align="right">
        <s:submit type="input" value="%{getText('back.label')}" onclick="goBack();"/>
    </div>
</div>
<div id="init-land-update-list">
    <div class="page-title">
        <h2><s:label value="%{getText('schedule_land_visits.label')}"/></h2>
    </div>
    <s:if test="hasActionErrors() || hasActionMessages()">
        <div id="message-box" class="message-box">
            <div class="action-msg"><s:actionmessage/></div>
            <div class="action-err"><s:actionerror/></div>
        </div>
    </s:if>
    <s:form id="schedule-land-visit-plan" name="schedule-land-visit-plan"
            method="POST" onsubmit="javascript:return validateFormForInputDataSyntaxErrors('schedule-land-visit-plan')">
        <table class="width-500-px margin-10-auto">
            <tr>
                <td><s:label value="%{getText('district.label')}"/></td>
                <td><s:textfield id="districtId" name="districtName" cssClass="width-200-px" disabled="true"/></td>
            </tr>
            <tr>
                <td><s:label value="%{getText('ds_division.label')}"/></td>
                <td><s:textfield id="dsDivisionId" name="dsDivisionName" cssClass="width-200-px" disabled="true"/></td>
            </tr>
            <tr>
                <td><s:label value="%{getText('gn_division.label')}"/></td>
                <td><s:select id="gnDivisionId" name="gnDivisionId" list="gnDivisionList" cssClass="width-200-px"/></td>
            </tr>
        </table>

        <h3 align="left"><s:label value="%{getText('land_list.label')}"/></h3>
        <table class="width-100 margin-10-auto table land-table">

        </table>
        <br/>
        <fieldset id="land-schedule-form" style="border:none;">
            <table class="width-100">
                <tr>
                    <td><s:label value="%{getText('schedule_date.label')}"/></td>
                    <td colspan="3"><s:textfield id="endingDate" name="schedule.endDate"
                                                 onKeyPress="return dateNumbersOnly(event,true)"/></td>
                </tr>
                <tr>
                    <td><s:label value="%{getText('task.label')}" cssClass="mandatory-field"/></td>
                    <td colspan="3"><s:textarea id="task" name="schedule.task" cssClass="width-610-px"/></td>
                </tr>
            </table>
            <br/>

            <div class="form-button-div right-align">
                <s:submit action="scheduleLandVisitPlan" value="%{getText('save.label')}"/>
                <s:submit cssClass="cancel" action="landRegistrationModule" value="%{getText('back.label')}"/>
            </div>
        </fieldset>
    </s:form>
</div>

<hr/>
<div id="view-scheduled-land-visits-div">
    <h3 align="left"><s:label value="%{getText('scheduled_list.label')}"/></h3>

    <table class="width-100 margin-10-auto table land-table-schedule">
        <thead>
        <tr class="left-align table-header">
            <th class="left-align"><s:label value="Id"/>&nbsp;</th>
            <th class="left-align" width="150px"><s:label value="%{getText('gn_division.label')}"/></th>
            <th class="left-align" width="100px"><s:label value="%{getText('schedule_date.label')}"/></th>
            <th class="left-align"><s:label value="%{getText('land_names.label')}"/></th>
            <th class="left-align" width="60px"><s:label value="%{getText('delete.label')}"/></th>
        </tr>
        </thead>
        <tbody>
        <s:iterator status="status1" value="scheduleList">
            <tr class="<s:if test="#status1.odd == true">table-row-odd</s:if><s:else>table-row-even</s:else>t">
                <td class="left-align"><s:label value="%{#status1.count}"/></td>
                <td class="left-align"><s:property value="gnDivision.enDivisionName"/></td>
                <td class="center-align"><s:property value="endDate"/></td>
                <td class="left-align">
                    <s:iterator status="status1" value="landList">
                        <s:property value="nameOfTheLand"/></br>
                    </s:iterator>
                </td>
                <td class="center-align">
                    <s:url id="deleteSchedule" action="deleteScheduledLandVisits">
                        <s:param name="scheduleId" value="id"/>
                    </s:url>
                    <s:a href="%{deleteSchedule}" title="%{getText('delete_schedule.label') } ">
                        <img src="<s:url  value='/images/reject.png'/>" border="none" width="25"
                             height="25"/>
                    </s:a>
                </td>
            </tr>
        </s:iterator>
        </tbody>
    </table>
</div>


<%-- Localization text for java script generated text --%>
<%--<s:hidden id="ref" value="%{getText('reference_no.label')}"/>--%>
<s:hidden id="ref" value="%{getText('slmsSerialNumber.label')}"/>
<s:hidden id="lname" value="%{getText('name_of_the_land.label')}"/>
<s:hidden id="plan" value="%{getText('plan_no.label')}"/>
<s:hidden id="lot" value="%{getText('lot_no.label')}"/>
<s:hidden id="extent" value="%{getText('extent_perches.label')}"/>
<s:hidden id="select" value="%{getText('select.label')}"/>
<s:hidden id="viewTooltip" value="%{getText('schedule_tooltip')}"/>
<s:hidden id="undifined" value="%{getText('undefined.label')}"/>

<%-- Localization text for data tables --%>
<s:hidden id="display" value="%{getText('display.label')}"/>
<s:hidden id="records" value="%{getText('records.label')}"/>
<s:hidden id="search" value="%{getText('search.label')}"/>
<s:hidden id="no-record" value="%{getText('no_record.label')}"/>
<s:hidden id="showing" value="%{getText('showing.label')}"/>
<s:hidden id="to" value="%{getText('to.label')}"/>
<s:hidden id="of" value="%{getText('of.label')}"/>
<s:hidden id="entries" value="%{getText('entries.label')}"/>
<s:hidden id="first" value="%{getText('first.label')}"/>
<s:hidden id="last" value="%{getText('last.label')}"/>
<s:hidden id="next" value="%{getText('next.label')}"/>
<s:hidden id="previous" value="%{getText('previous.label')}"/>
<s:hidden id="pls_enter_task" value="%{getText('pls_enter_task.label')}"/>
<s:hidden id="pls_enter_date" value="%{getText('pls_enter_valid_date.label')}"/>
